package main

import (
	"context"
	"fmt"
	"gitee.com/jinmingzhi/eth-learn/pkg/ethtool"
	"github.com/ethereum/go-ethereum/common"
)

const url = "http://localhost:8545"

var client *ethtool.Client

func main() {
	var err error
	client, err = ethtool.Dial(url)
	assert(err)
	ctx := context.Background()
	//addresses, err := client.EthAccounts(ctx)
	//assert(err)
	//msg := map[string]interface{}{
	//	"from":  addresses[3],
	//	"to":    addresses[5],
	//	"value": ethtool.ToWei(big.NewFloat(1.3), ethtool.Ether),
	//}
	//
	//txid, err := client.EthSendTransaction(ctx, msg)
	//assert(err)
	//fmt.Println(txid.Hex())
	//
	//balance, err := client.EthGetBalance(ctx, addresses[5], "latest")
	//assert(err)
	//fmt.Printf("address[5]: %s, balance@latest: %f\n", addresses[5], ethtool.FromWei(balance, ethtool.Ether))
	//
	WaitTransaction(ctx, common.HexToHash("0x82e8dd2eb6ae5f1328c348111a2e694840efd64ca25a90e4491dbb369376f8d7"))
}

func assert(err error) {
	if err != nil {
		panic(err)
	}
}

func WaitTransaction(ctx context.Context, txid common.Hash) {
	receipt, err := client.EthGetTransactionReceipt(ctx, txid)
	assert(err)
	fmt.Println("blockHash:",receipt.BlockHash)
	fmt.Println("BlockNumber:",receipt.BlockNumber)
	fmt.Println("TransactionIndex:",receipt.TransactionIndex)
	fmt.Println("CumulativeGasUsed:",receipt.CumulativeGasUsed)
	fmt.Println("GasUsed:",receipt.GasUsed)
	fmt.Println("Logs:",receipt.Logs)
	fmt.Println("ContractAddress:",receipt.ContractAddress)
}
